﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.UI;

namespace Univar
{
    public static partial class Storage
    {
        public static class Cache
        {
            public static TimeSpan DefaultLifeTime = TimeSpan.FromMinutes(20);

            public static string Get(string key)
            {
                return Get<string>(key, null);
            }

            public static T Get<T>(string key)
            {
                return Get<T>(key, null);
            }

            /// <summary>
            /// Gets the object of type T stored under a specific key.
            /// </summary>
            /// <typeparam name="T">The object type.</typeparam>
            /// <param name="key">The key under which the value is to be saved.</param>
            /// <param name="HttpContext">The HttpContext from which the request is made. This is only required during asynchronous operations where HttpRuntime.Cache is null.</param>
            /// <returns></returns>
            public static T Get<T>(string key, HttpContext HttpContext)
            {
                object value = (HttpRuntime.Cache ?? HttpContext.Cache)[key];
                return value == null ? default(T) : (T)value;
            }

            public static void Set(string key, object value)
            {
                Set<object>(key, value, null, DefaultLifeTime, null, CacheItemPriority.Normal, null);
            }

            public static void Set<T>(string key, T value)
            {
                Set<T>(key, value, null, DefaultLifeTime, null, CacheItemPriority.Normal, null);
            }

            /// <summary>
            /// Saves a value to the cache. The key associated with the value is associated with the session ID such that
            /// it is not shared with other users.
            /// </summary>
            /// <typeparam name="T">The object type.</typeparam>
            /// <param name="key">The key under which the value is to be saved.</param>
            /// <param name="value">The value to be saved.</param>
            /// <param name="HttpContext">The HttpContext from which the request is made. This is only required during asynchronous operations where HttpRuntime.Cache is null.</param>
            /// <param name="lifeTime">The lifetime of the cache object.</param>
            /// <param name="cacheDependencies">The file or cache key dependencies for the cache object that determine when it must be removed.</param>
            /// <param name="cacheItemPriority">The cache priority that the cache object is given to determine when it must be removed.</param>
            /// <param name="cacheItemRemovedCallback">The event that is called whenever the cache object is removed.</param>
            public static void Set<T>(string key, T value, HttpContext HttpContext, TimeSpan lifeTime, CacheDependency cacheDependencies, CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback)
            {
                if (value != null)
                    (HttpRuntime.Cache ?? HttpContext.Cache).Insert(key, value, cacheDependencies, DateTime.Now.Add(lifeTime),
                      System.Web.Caching.Cache.NoSlidingExpiration, cacheItemPriority, cacheItemRemovedCallback);
                else
                    HttpRuntime.Cache.Remove(key);

            }


        }
    }
}